package com.zhongwang.mms.module.material.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.module.basic.dao.TWarehouseInfoDao;
import com.zhongwang.mms.module.material.dao.MaterApplyDao;
import com.zhongwang.mms.module.material.model.MAdjustModel;
import com.zhongwang.mms.module.material.model.MinventorytModel;
import com.zhongwang.mms.module.material.model.MmaterialsDic;
import com.zhongwang.mms.module.material.service.MateApplyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 库存调整申请 控制层
 * @Author: China.mc
 * @Date: 2020/3/21
 */
@Controller
@RequestMapping("/material/matInvManage/rawMatJustApply")
public class MateApplyController extends BaseController {

    @Autowired
    private MateApplyService mateApplyService;
    @Autowired
    private MaterApplyDao materApplyDao;
    @Autowired
    private TWarehouseInfoDao tWarehouseInfoDao;

    /**
     * 页面路径
     *
     * @return
     */
    @GetMapping
    @RequiresPermissions({"matinv:matadjlist:view"})
    public String project() {
        return "material/matInvManage/rawMatJustApply/page";
    }

    /**
     * 库存调整申请加载列表
     *
     * @param madjustModel
     * @return
     */
    @ResponseBody
    @PostMapping("/onelist")
    @RequiresPermissions({"matinv:matadjlist:view"})
    public JsonResult onelist(MAdjustModel madjustModel) {
        startPage();
        List<MAdjustModel> oneList = mateApplyService.onefindProjectPage(madjustModel);
        return jsonResult(oneList);
    }


    /**
     * 删除未提交数据
     *
     * @param madjustModel
     * @return
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"matinv:matadjlist:edit"})
    public JsonResult del(MAdjustModel madjustModel) {
        return jsonResult(mateApplyService.delProjectStatus(madjustModel));
    }


    /**
     * 库存调整申请查看
     *
     * @param adjId
     * @param model
     * @return
     */
    @GetMapping("aaa/{adjId}")
    public String project(@PathVariable("adjId") Integer adjId, Model model) {
        MAdjustModel applySel = materApplyDao.findAdjByOderCode(adjId);
        applySel.getAdjOrderCode();
        applySel.getAdjId();
        MAdjustModel adjust = null;
        MAdjustModel advive = null;
        MAdjustModel advivetow = null;
        advive = materApplyDao.SelfindAdjByOderCode(applySel);
        advivetow = materApplyDao.findAdjByOderCodeTow(applySel);
        adjust = mateApplyService.findviewtDailyId(applySel);
        //调整物料与单价时合并操作列进行判断查看
        if (advive != null && advive.getAdjOditStatus() != null && advive.getAdjOditStatus() == 2 && advive.getAdjBeAfFlag() != null && advive.getAdjBeAfFlag() == 1) {
            model.addAttribute("advive", advive);
        }
        //调整物料前的物料信息
        if (advivetow != null && advivetow.getAdjOditStatus() != null && advivetow.getAdjOditStatus() == 2 && advivetow.getAdjBeAfFlag() != null && advivetow.getAdjBeAfFlag() == 2) {
            model.addAttribute("advivetow", advivetow);
        }
        //修改在库数量时进行查看
        if (adjust != null && adjust.getAdjOditStatus() == null && adjust.getAdjOrderCode() == null && adjust.getAdjBeAfFlag() == null) {
            model.addAttribute("adjust", adjust);
        }
        model.addAttribute("check", applySel);
        return "material/matInvManage/rawMatJustApply/view";
    }


    /**
     * 提交待审核数据
     *
     * @param madjustModel
     * @return
     */
    @ResponseBody
    @PostMapping("/and")
    @RequiresPermissions({"matinv:matadjlist:edit"})
    public JsonResult and(MAdjustModel madjustModel) {
        return jsonResult(mateApplyService.updateProjectStatusand(madjustModel));
    }

    /**
     * 库存调整申请时通过adjid回显调整数据
     *
     * @param adjId
     * @return
     */
    @ResponseBody
    @PostMapping("/adjId")
    @RequiresPermissions({"matinv:matadjlist:view"})
    public MAdjustModel getByUpdateAdjId(Integer adjId) {
        return mateApplyService.getByUpdateAdjId(adjId);
    }

    /**
     * 保存库存修改数据
     *
     * @param mAdjustModel
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions({"matinv:matadjlist:view"})
    public JsonResult getByUpdateAdjIdAdd(MAdjustModel mAdjustModel) {
        return jsonResult(mateApplyService.getByUpdateAdjIdAdd(mAdjustModel));
    }

    /**
     * 创建库存调整页面
     *
     * @param model
     * @return
     */
    @GetMapping("/create")
    @RequiresPermissions({"matinv:matadjlist:view"})
    public String create(Model model) {

        model.addAttribute("warehouse", tWarehouseInfoDao.findWarehouseInfoListByWhiType("原料库"));
        return "material/matInvManage/rawMatJustApply/create";
    }

    /**
     * 根据ityId查询库存信息库存调整
     *
     * @param ityId
     * @return
     */
    @ResponseBody
    @PostMapping("/ityId")
    @RequiresPermissions({"matinv:matadjlist:view"})
    public MinventorytModel ityId(Integer ityId) {
        return mateApplyService.getById(ityId);
    }

    /**
     * 根据id调整库存信息进行保存
     *
     * @param minventorytModel
     * @return
     */
    @ResponseBody
    @PostMapping("/insert")
    @RequiresPermissions({"matinv:matadjlist:edit"})
    public JsonResult insertCreate(MinventorytModel minventorytModel) {
        JsonResult jsonResult = new JsonResult();
        Integer res = mateApplyService.insterProject(minventorytModel);
        // 申请调整时在库数量与可用数量不等时，不允许保存
        if (res == 0) {
            return jsonResult.fail("在库数量与可用数量不等，不能调整该库存信息！");
        }
        return jsonResult.success("操作成功！");
    }

    /**
     * 根据id查询物料基础信息
     *
     * @param mmaterialsDic
     * @return
     */
    @ResponseBody
    @PostMapping("/MateEnquiryList")
    @RequiresPermissions({"matinv:matadjlist:view"})
    public JsonResult list(MmaterialsDic mmaterialsDic) {
        startPage();
        List<MmaterialsDic> list = mateApplyService.materialmatId(mmaterialsDic);
        return jsonResult(list);
    }

    /**
     * 库存调整加载页面
     *
     * @param minventorytModel
     * @return
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"matinv:matadjlist:view"})
    public JsonResult list(MinventorytModel minventorytModel) {
        startPage();
        List<MinventorytModel> list = mateApplyService.selectByPrimaryKey(minventorytModel);
        return jsonResult(list);
    }

    /**
     * 添加新库存
     *
     * @param madjustModel
     * @return
     */
    @ResponseBody
    @PostMapping("/insertSt")
    @RequiresPermissions({"matinv:matadjlist:edit"})
    public JsonResult insertStock(MAdjustModel madjustModel) {

        return jsonResult(mateApplyService.insertStock(madjustModel));
    }

    /**
     * 库存调整申请批量提交
     *
     * @param arr Id集合
     * @return
     */
    @ResponseBody
    @PostMapping("/togetherAnd")
    @RequiresPermissions({"matinv:matadjlist:edit"})
    public JsonResult updateProjectStatustogetherAnd(@RequestBody List<Integer> arr) {

        mateApplyService.updateProjectStatustogetherAnd(arr);
        return success();
    }

}



